/**
 * 创建屋顶,将创建长xWidth，宽yWidth，屋顶高度height的模型，该模型底面中心点在坐标原点(0,0,0),
 */
function createTriangularRoof(xWidth, yWidth, height) {
    let roofPoints = [[-yWidth / 2, 0, 0], [yWidth / 2, 0, 0], [0, height, 0]]; // 三角形屋顶的顶点（XoY平面）
    let roofPolygon = Polygon(roofPoints); // 创建三角形多边形
    let roof = Extrude(roofPolygon, [0, 0, xWidth]); // 将三角形拉伸成棱柱
    roof = Rotate([1, 0, 0], 90, roof); // 将棱柱绕X轴旋转90度，使其垂直于XoY平面
    roof = Rotate([0, 0, 1], 90, roof); // 将棱柱绕X轴旋转90度，使其垂直于XoY平面
    roof = Translate([-xWidth / 2, 0, 0], roof); // 将屋顶移动到房子的顶部

    return roof
}


function AgBox(width, height, depth) {
    let houseBody = Box(width, height, depth, false); // 长方体，尺寸为100x100x50，不居中
    houseBody = Translate([-width / 2, -height / 2, 0], houseBody); // 将长方体移动到正确的位置
    return houseBody
}

function createBalcony(len, translate) {
    let height = 10;
    let width = 16;
    // 重新定位阳台系统（Z轴下降10单位）
    let balconyBase = Translate([0, 0, 0], AgBox(len, width, 2)); // 底座位于二层顶部下方10单位

    // 栏杆系统（高度对齐新位置）
    let frontRailing = Translate([0, height - 2, 0], AgBox(len, 2, height)); // Z轴与底座对齐
    let leftRailing = Translate([-len / 2, 0, 0], AgBox(2, width, height));
    let rightRailing = Translate([len / 2, 0, 0], AgBox(2, width, height));

    // 立柱系统（缩短高度）
    let pillar1 = Translate([-len / 2, height - 2, 0], Cylinder(1, height + 2)); // 高度5单位
    let pillar2 = Translate([len / 2, height - 2, 0], Cylinder(1, height + 2));
    let pillar3 = Translate([-len / 2, -height + 2, 0], Cylinder(1, height + 2));
    let pillar4 = Translate([len / 2, -height + 2, 0], Cylinder(1, height + 2));

    let a = [balconyBase, frontRailing, leftRailing, rightRailing, pillar1, pillar2, pillar3, pillar4];
    translate[1] += height - 2;
    for (var i = 0; i < a.length; i++) {
        a[i] = Translate(translate, a[i], false)
    }
}
/**
 * 创建女儿墙
 * @param {*} width 
 * @param {*} height 
 * @param {*} translate 
 * @returns 
 */
function createParapet(width, height, translate){
    let parapet = AgBox(width + 2, height+2, 5); // 女儿墙稍微大一点
    parapet = Difference(parapet, [AgBox(width - 2, height-2, 5)]); // 挖空内部
    parapet = Translate(translate, parapet); // 放置在屋顶上方
    return parapet;
}
function createDoor(doorWidth, doorHeight, doorDepth) {
    // 创建门的主体
    //let doorWidth = 10;
    //let doorHeight = 20;
    //let doorDepth = 2;
    let door = AgBox(doorWidth, doorDepth, doorHeight);

    // 创建门把手
    let handleRadius = 0.5;
    let handleHeight = 2;
    let handle = Cylinder(handleRadius, handleHeight);
    let rotatedHandle = Rotate([1, 0, 0], 90, handle); // 将圆柱体旋转为水平方向
    let translatedHandle = Translate([-doorWidth / 3, doorDepth * 2 / 3, doorHeight / 2], rotatedHandle); // 将把手放置在门的一侧

    // 组合门的各个部分
    let elegantDoor = Union([door, translatedHandle]);

    return elegantDoor;
}
function createWindow(windowWith, windowHeight, translate) {
    // 创建窗户框架

    let allS = [];

    let windowFrame = AgBox(windowWith, 2, windowHeight); // 窗户框架尺寸为20x2x30
    allS.push(windowFrame);

    // 创建窗户横梁
    let windowCrossbar = AgBox(windowWith, 2, 1); // 横梁尺寸为20x2x2
    for (var i = 0; i <= windowHeight; i += 5) {
        let windowCrossbar1 = Translate([0, 0.5, i], windowCrossbar); // 将横梁放置在窗户中间
        allS.push(windowCrossbar1);
    }

    // 创建窗户竖梁
    let windowVerticalBar = AgBox(1, 2, windowHeight); // 竖梁尺寸为2x2x30

    let split = (windowWith - 1) / 2.0;
    for (var i = 0; i < windowWith; i += split) {
        let windowVerticalBar2 = Translate([windowWith / 2 - i - 0.5, 0.5, 0], windowVerticalBar); // 将竖梁放置在右侧
        allS.push(windowVerticalBar2);
    }
    for (var i = 0; i < allS.length; i++) {
        allS[i] = Translate(translate, allS[i], false)
    }
}